import XLSX from '@./xlsx.full.min.js'
const tableData = [{
		num: '3322',
		name: '王淇',
		playertime: '3.32',
		org: '单位1',
		selected: false,
	},
	{
		num: '3323',
		name: '田鹏',
		playertime: '2.22',
		rank: '1',
		selected: false
	},
	{
		num: '3321',
		name: '陈海峰',
		playertime: '',
		rank: '1',
		selected: false
	}, {
		num: '3320',
		name: '梁婷维',
		playertime: '',
		rank: '1',
		selected: false
	}, {
		num: '3322',
		name: '王淇',
		playertime: '',
		rank: '1',
		selected: false
	},
	{
		num: '3322',
		name: '王淇',
		playertime: '',
		rank: '1',
		selected: false
	},
	{
		num: '3322',
		name: '王淇',
		playertime: '',
		rank: '1',
		selected: false
	},
	{
		num: '3322',
		name: '王淇',
		playertime: '',
		rank: '1',
		selected: false
	},
]
const ws = XLSX.utils.json_to_sheet(tableData)
	const whtml = XLSX.utils.sheet_to_html(ws)
	const worksheet = 'sheet1'
	const template = `<html xmlns:o="urn:schemas-microsoft-com:office:office"
xmlns:x="urn:schemas-microsoft-com:office:excel"
xmlns="http://www.w3.org/TR/REC-html40">
<head><!--[if gte mso 9]><xml encoding="UTF-8"><x:ExcelWorkbook><x:ExcelWorksheets><x:ExcelWorksheet>
<x:Name>${worksheet}</x:Name>
<x:WorksheetOptions><x:DisplayGridlines/></x:WorksheetOptions></x:ExcelWorksheet>
</x:ExcelWorksheets></x:ExcelWorkbook></xml><![endif]-->
</head><body><table>${whtml}</table></body></html>`
	console.log('whtml', whtml);
	plus.io.requestFileSystem(plus.io.PUBLIC_DOCUMENTS, fs => {
		fs.root.getFile('测试表格.xls', {create: true}, fileEntry => {
			fileEntry.createWriter(writer => {
				writer.onwrite = e => {
					uni.showModal({
						title: '提示',
						content: `导出成功，文件路径为${e.target.fileName}`,
						cancelText: '关闭',
						confirmText: '预览',
						success: res => {
							if ( res.confirm ) {
								uni.openDocument({
									filePath: `file://${e.target.fileName}`
								})
							}
						}
					})
				}
				writer.write(template)
			}, err => {
				console.log('createWritererr', err);
				uni.showToast({
					title: '导出文件失败，请检查是否有权限',
					icon: 'none'
				})
			})
		}, err => {
			console.log('fileEntryerr', err);
			uni.showToast({
				title: '导出文件失败，请检查是否有权限',
				icon: 'none'
			})
		})
	}, err => {
		console.log('PUBLIC_DOCUMENTSerr', err);
		uni.showToast({
			title: '导出文件失败，请检查是否有权限',
			icon: 'none'
		})
	})